package com.impethus.seguridadcolectiva.extras;

import java.util.Timer;
import java.util.TimerTask;

import org.osmdroid.ResourceProxy;
import org.osmdroid.tileprovider.MapTileProviderBase;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyCustomMapView extends MapView {

	public MyCustomMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyCustomMapView(Context context, int tileSizePixels) {
		super(context, tileSizePixels);
		// TODO Auto-generated constructor stub
	}

	public MyCustomMapView(Context context, int tileSizePixels,
			ResourceProxy resourceProxy) {
		super(context, tileSizePixels, resourceProxy);
		// TODO Auto-generated constructor stub
	}

	public MyCustomMapView(Context context, int tileSizePixels,
			ResourceProxy resourceProxy, MapTileProviderBase aTileProvider) {
		super(context, tileSizePixels, resourceProxy, aTileProvider);
		// TODO Auto-generated constructor stub
	}

	public MyCustomMapView(Context context, int tileSizePixels,
			ResourceProxy resourceProxy, MapTileProviderBase aTileProvider,
			Handler tileRequestCompleteHandler) {
		super(context, tileSizePixels, resourceProxy, aTileProvider,
				tileRequestCompleteHandler);
		// TODO Auto-generated constructor stub
	}

	// protected MyCustomMapView(Context arg0, int arg1, ResourceProxy arg2,
	// MapTileProviderBase arg3, Handler arg4, AttributeSet arg5) {
	// super(arg0, arg1, arg2, arg3, arg4, arg5);
	// // TODO Auto-generated constructor stub
	// }

	// Define the interface we will interact with from our Map
	public interface OnLongpressListener {
		public void onLongpress(MapView view, GeoPoint longpressLocation);
	}

	/**
	 * Time in ms before the OnLongpressListener is triggered.
	 */
	static final int LONGPRESS_THRESHOLD = 1000;

	/**
	 * Keep a record of the center of the map, to know if the map has been
	 * panned.
	 */
	private GeoPoint lastMapCenter;

	private Timer longpressTimer = new Timer();
	private MyCustomMapView.OnLongpressListener longpressListener;

	public void setOnLongpressListener(
			MyCustomMapView.OnLongpressListener listener) {
		longpressListener = listener;
	}

	/**
	 * This method is called every time user touches the map, drags a finger on
	 * the map, or removes finger from the map.
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		handleLongpress(event);

		return super.onTouchEvent(event);
	}

	/**
	 * This method takes MotionEvents and decides whether or not a longpress has
	 * been detected. This is the meat of the OnLongpressListener.
	 * 
	 * The Timer class executes a TimerTask after a given time, and we start the
	 * timer when a finger touches the screen.
	 * 
	 * We then listen for map movements or the finger being removed from the
	 * screen. If any of these events occur before the TimerTask is executed, it
	 * gets cancelled. Else the listener is fired.
	 * 
	 * @param event
	 */
	private void handleLongpress(final MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// Finger has touched screen.
			final int x = (int) event.getX();
			final int y = (int) event.getY();
			longpressTimer = new Timer();
			longpressTimer.schedule(new TimerTask() {
				/*
				 * (non-Javadoc)
				 * 
				 * @see java.util.TimerTask#run()
				 */
				@Override
				public void run() {
					GeoPoint longpressLocation = (GeoPoint) getProjection()
							.fromPixels(x, y);

					/*
					 * Fire the listener. We pass the map location of the
					 * longpress as well, in case it is needed by the caller.
					 */
					longpressListener.onLongpress(MyCustomMapView.this,
							longpressLocation);
				}

			}, LONGPRESS_THRESHOLD);

			lastMapCenter = (GeoPoint) getMapCenter();
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE) {

			if (!getMapCenter().equals(lastMapCenter)) {
				// User is panning the map, this is no longpress
				longpressTimer.cancel();
			}

			lastMapCenter = (GeoPoint) getMapCenter();
		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			// User has removed finger from map.
			longpressTimer.cancel();
		}

		if (event.getPointerCount() > 1) {
			// This is a multitouch event, probably zooming.
			longpressTimer.cancel();
		}
	}
}